package ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;

import dao.DBOperation;
import export.ExportabilityImp;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.awt.event.ActionEvent;
import net.miginfocom.swing.MigLayout;
import pojo.Company;
import pojo.SelectedItems;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JCheckBox;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.JComboBox;
import javax.swing.JSeparator;
import java.io.File;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JRadioButton;

import util.*;

public class ExportDialog extends JDialog {

	private JCheckBox checkBox;		
	private JCheckBox checkBox_1;		
	private JCheckBox checkBox_4;		
	private JCheckBox checkBox_5;		
	private JCheckBox checkBox_6;		
	private JCheckBox checkBox_7;		
	private JCheckBox checkBox_8;		
	private JCheckBox checkBox_9;		
	private JCheckBox checkBox_10;				
	private JCheckBox checkBox_11;		
	private JCheckBox checkBox_12;		
	private JCheckBox checkBox_13;		
	private JCheckBox checkBox_14;		
	private JCheckBox checkBox_15;	
	private JCheckBox checkBox_16;	
	private JCheckBox checkBox_17;
	private JCheckBox checkBox_18;
	private JCheckBox checkBox_19;
	private JCheckBox checkBox_20;
	private JCheckBox checkBox_21;
	private JCheckBox checkBox_23;

	private final JPanel contentPanel = new JPanel();
	private JCheckBox checkBox_24;
	private JCheckBox checkBox_2;
	private JLabel label_1;
	private JRadioButton radioButton_1;
	private JRadioButton radioButton_2;
	private JRadioButton radioButton_3;
	private JRadioButton radioButton_4;
	private JRadioButton radioButton_5;
	private JRadioButton radioButton_6;
	private JRadioButton radioButton_7;
	private JRadioButton radioButton_8;
	private  ButtonGroup group1;
	private  ButtonGroup group2;
	private JSeparator separator_3;
	private JFileChooser fc;
	private DBOperation dbOperation;
	private ExportabilityImp exp;
	private JCheckBox checkBox_3;
	private JCheckBox checkBox_22;
	private JRadioButton radioButton;
	private JRadioButton radioButton_9;
	private JCheckBox checkBox_25;
	private JCheckBox checkBox_26;
	private JSeparator separator_4;
	private JLabel label_2;
	private JRadioButton radioButton_20;
	private JRadioButton radioButton_10;
	private JRadioButton radioButton_11;
	private JRadioButton radioButton_12;
	private JRadioButton radioButton_13;
	private JRadioButton radioButton_14;
	private JRadioButton radioButton_15;
	private JRadioButton radioButton_16;
	private JRadioButton radioButton_17;
	private JRadioButton radioButton_18;
	private JRadioButton radioButton_19;
	private JSeparator separator_5;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			initGlobalFont();
			ExportDialog dialog = new ExportDialog();
			dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ExportDialog() {
		dbOperation = new DBOperation();
		exp = new ExportabilityImp();
		setTitle("导出统计");
//		setLocation(0, 0);
		setBounds(100, 100, 546, 646);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[120px,grow][][][]", "[15px][][][][][][][][][][][][][][][][][][][][][][][][][]"));

		JLabel label = new JLabel("选择导出的内容：");
		contentPanel.add(label, "cell 0 0,alignx left,aligny center");

		checkBox = new JCheckBox("基本信息");
		checkBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
					selectAllJibenxinxi();
				else
					deselectAllJibenxinxi();
			}
		});
		
		separator_5 = new JSeparator();
		contentPanel.add(separator_5, "cell 0 1 4 1,growx");
		contentPanel.add(checkBox, "cell 0 2");

		checkBox_1 = new JCheckBox("企业名称");
		contentPanel.add(checkBox_1, "cell 0 3");

		checkBox_25 = new JCheckBox("建设单位");
		contentPanel.add(checkBox_25, "cell 1 3");

		checkBox_4 = new JCheckBox("占地");
		contentPanel.add(checkBox_4, "cell 2 3");

		checkBox_5 = new JCheckBox("注册资本");
		contentPanel.add(checkBox_5, "cell 3 3");

		checkBox_6 = new JCheckBox("总投资");
		contentPanel.add(checkBox_6, "cell 0 4");

		checkBox_7 = new JCheckBox("年产值");
		contentPanel.add(checkBox_7, "cell 1 4");

		checkBox_3 = new JCheckBox("利润");
		contentPanel.add(checkBox_3, "cell 2 4");

		checkBox_22 = new JCheckBox("税收");
		contentPanel.add(checkBox_22, "cell 3 4");

		checkBox_8 = new JCheckBox("用工");
		contentPanel.add(checkBox_8, "cell 0 5");

		checkBox_9 = new JCheckBox("备注");
		contentPanel.add(checkBox_9, "cell 1 5");

		checkBox_10 = new JCheckBox("简介");
		contentPanel.add(checkBox_10, "cell 2 5");

		checkBox_2 = new JCheckBox("项目属性");
		contentPanel.add(checkBox_2, "cell 3 5");
		
		checkBox_26 = new JCheckBox("企业分类");
		checkBox_26.setSelected(true);
		contentPanel.add(checkBox_26, "cell 0 6");

		JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
		contentPanel.add(separator, "cell 0 7 4 1,growx");

		checkBox_11 = new JCheckBox("手续办理时间");
		checkBox_11.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
					selectAllShouxubanlishijian();
				else
					deselectAllShouxubanlishijian();
			}
		});
		contentPanel.add(checkBox_11, "cell 0 8");

		checkBox_12 = new JCheckBox("注册");
		contentPanel.add(checkBox_12, "cell 0 9");

		checkBox_13 = new JCheckBox("土地预审");
		contentPanel.add(checkBox_13, "cell 1 9");

		checkBox_14 = new JCheckBox("备案");
		contentPanel.add(checkBox_14, "cell 2 9");

		checkBox_15 = new JCheckBox("环评");
		contentPanel.add(checkBox_15, "cell 3 9");

		checkBox_24 = new JCheckBox("土地出让合同");
		contentPanel.add(checkBox_24, "cell 0 10");

		checkBox_16 = new JCheckBox("建设用地规划许可");
		contentPanel.add(checkBox_16, "cell 1 10");

		checkBox_17 = new JCheckBox("建筑工程规划许可");
		contentPanel.add(checkBox_17, "cell 2 10");

		checkBox_18 = new JCheckBox("施工许可");
		contentPanel.add(checkBox_18, "cell 3 10");

		JSeparator separator_1 = new JSeparator();
		contentPanel.add(separator_1, "cell 0 11 4 1,growx");

		checkBox_19 = new JCheckBox("年度目标");
		checkBox_19.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
					selectAllNiandumubiao();
				else
					deselectAllNiandumubiao();
			}
		});
		contentPanel.add(checkBox_19, "cell 0 12");

		checkBox_20 = new JCheckBox("完成投资");
		contentPanel.add(checkBox_20, "cell 0 13");

		checkBox_21 = new JCheckBox("开工面积");
		contentPanel.add(checkBox_21, "cell 1 13");

		JSeparator separator_2 = new JSeparator();
		contentPanel.add(separator_2, "cell 0 14 4 1,growx");

		checkBox_23 = new JCheckBox("当前进展");
		contentPanel.add(checkBox_23, "cell 0 15");

		separator_3 = new JSeparator();
		contentPanel.add(separator_3, "cell 0 16 4 1,growx");

		label_1 = new JLabel("选择项目属性：");
		contentPanel.add(label_1, "cell 0 17");
		
		radioButton_18 = new JRadioButton("所有");
		radioButton_18.setSelected(true);
		contentPanel.add(radioButton_18, "cell 1 17");

		radioButton_1= new JRadioButton("青岛市重点项目");
		contentPanel.add(radioButton_1, "cell 0 18");

		radioButton_2 = new JRadioButton("胶州市重点项目");
		contentPanel.add(radioButton_2, "cell 1 18");

		radioButton_3 = new JRadioButton("投资过亿元项目");
		contentPanel.add(radioButton_3, "cell 2 18");

		radioButton_4 = new JRadioButton("青岛以外内资项目");
		contentPanel.add(radioButton_4, "cell 3 18");

		radioButton_5 = new JRadioButton("战略新兴项目");
		contentPanel.add(radioButton_5, "cell 0 19");

		radioButton_6 = new JRadioButton("亿元新开工项目");
		contentPanel.add(radioButton_6, "cell 1 19");

		radioButton_7 = new JRadioButton("服务业重点项目");
		contentPanel.add(radioButton_7, "cell 2 19");

		radioButton_8 = new JRadioButton("总部经济项目");
		contentPanel.add(radioButton_8, "cell 3 19");
		
		radioButton = new JRadioButton("内资");
		contentPanel.add(radioButton, "cell 0 20");

		radioButton_9 = new JRadioButton("外资");
		contentPanel.add(radioButton_9, "cell 1 20");

		group1 = new ButtonGroup();
		group1.add(radioButton_1);
		group1.add(radioButton_2);
		group1.add(radioButton_3);
		group1.add(radioButton_4);
		group1.add(radioButton_5);
		group1.add(radioButton_6);
		group1.add(radioButton_7);
		group1.add(radioButton_8);
		group1.add(radioButton);
		group1.add(radioButton_9);
		group1.add(radioButton_18);
		
		separator_4 = new JSeparator();
		contentPanel.add(separator_4, "cell 0 21 4 1,growx");
		
		label_2 = new JLabel("选择企业分类：");
		contentPanel.add(label_2, "cell 0 22");
		
		radioButton_19 = new JRadioButton("所有");
		radioButton_19.setSelected(true);
		contentPanel.add(radioButton_19, "cell 1 22");
		
		radioButton_20 = new JRadioButton("工业");
		contentPanel.add(radioButton_20, "cell 0 23");
		
		radioButton_10 = new JRadioButton("建筑业");
		contentPanel.add(radioButton_10, "cell 1 23");
		
		radioButton_11 = new JRadioButton("交通运输仓储和邮政业");
		contentPanel.add(radioButton_11, "cell 2 23");
		
		radioButton_12 = new JRadioButton("批发业和零售业");
		contentPanel.add(radioButton_12, "cell 3 23");
		
		radioButton_13 = new JRadioButton("房地产业");
		contentPanel.add(radioButton_13, "cell 0 24");
		
		radioButton_14 = new JRadioButton("营利性服务业");
		contentPanel.add(radioButton_14, "cell 1 24");
		
		radioButton_15 = new JRadioButton("住宿业和餐饮业");
		contentPanel.add(radioButton_15, "cell 2 24");
		
		radioButton_16 = new JRadioButton("金融业");
		contentPanel.add(radioButton_16, "cell 3 24");
		
		radioButton_17 = new JRadioButton("非营利性服务业");
		contentPanel.add(radioButton_17, "cell 0 25");
		
		group2 = new ButtonGroup();
		group2.add(radioButton_10);
		group2.add(radioButton_11);
		group2.add(radioButton_12);
		group2.add(radioButton_13);
		group2.add(radioButton_14);
		group2.add(radioButton_15);
		group2.add(radioButton_16);
		group2.add(radioButton_17);
		group2.add(radioButton_19);
		group2.add(radioButton_20);

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						exportFile();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		selectAll();		
	}

	public static void showExportDialog() {
		initGlobalFont();
		ExportDialog dialog = new ExportDialog();
		dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
	}

	private void selectAll() {
		checkBox.setSelected(true);
		checkBox_1.setSelected(true);
		checkBox_2.setSelected(true);
		checkBox_3.setSelected(true);
		checkBox_4.setSelected(true);
		checkBox_5.setSelected(true);
		checkBox_6.setSelected(true);
		checkBox_7.setSelected(true);
		checkBox_8.setSelected(true);
		checkBox_9.setSelected(true);
		checkBox_10.setSelected(true);
		checkBox_11.setSelected(true);
		checkBox_12.setSelected(true);
		checkBox_13.setSelected(true);
		checkBox_14.setSelected(true);
		checkBox_15.setSelected(true);
		checkBox_16.setSelected(true);
		checkBox_17.setSelected(true);
		checkBox_18.setSelected(true);
		checkBox_19.setSelected(true);
		checkBox_20.setSelected(true);
		checkBox_21.setSelected(true);
		checkBox_22.setSelected(true);
		checkBox_23.setSelected(true);
		checkBox_24.setSelected(true);
		checkBox_25.setSelected(true);
		checkBox_26.setSelected(true);
	}

	private void selectAllJibenxinxi() {
		checkBox_1.setSelected(true);
		checkBox_2.setSelected(true);
		checkBox_3.setSelected(true);
		checkBox_4.setSelected(true);
		checkBox_5.setSelected(true);
		checkBox_6.setSelected(true);
		checkBox_7.setSelected(true);
		checkBox_8.setSelected(true);
		checkBox_9.setSelected(true);
		checkBox_10.setSelected(true);
		checkBox_22.setSelected(true);
		checkBox_25.setSelected(true);
		checkBox_26.setSelected(true);
	}

	private void deselectAllJibenxinxi() {
		checkBox_1.setSelected(false);
		checkBox_2.setSelected(false);
		checkBox_3.setSelected(false);
		checkBox_4.setSelected(false);
		checkBox_5.setSelected(false);
		checkBox_6.setSelected(false);
		checkBox_7.setSelected(false);
		checkBox_8.setSelected(false);
		checkBox_9.setSelected(false);
		checkBox_10.setSelected(false);
		checkBox_22.setSelected(false);
		checkBox_25.setSelected(false);
		checkBox_26.setSelected(false);
	}

	private void selectAllShouxubanlishijian() {
		checkBox_12.setSelected(true);
		checkBox_13.setSelected(true);
		checkBox_14.setSelected(true);
		checkBox_15.setSelected(true);
		checkBox_16.setSelected(true);
		checkBox_17.setSelected(true);
		checkBox_18.setSelected(true);
		checkBox_24.setSelected(true);
	}

	private void deselectAllShouxubanlishijian() {
		checkBox_12.setSelected(false);
		checkBox_13.setSelected(false);
		checkBox_14.setSelected(false);
		checkBox_15.setSelected(false);
		checkBox_16.setSelected(false);
		checkBox_17.setSelected(false);
		checkBox_18.setSelected(false);
		checkBox_24.setSelected(false);
	}

	private void selectAllNiandumubiao() {
		checkBox_20.setSelected(true);
		checkBox_21.setSelected(true);
	}

	private void deselectAllNiandumubiao() {
		checkBox_20.setSelected(false);
		checkBox_21.setSelected(false);
	}

	private SelectedItems buildSelectedItems() {
		SelectedItems items = new SelectedItems();
		items.setItems(new ArrayList<String>());
		//		if (checkBox.isSelected())
		//			items.getItems().add("基本信息");
		if (checkBox_1.isSelected())
			items.getItems().add("企业名称");
		if (checkBox_25.isSelected())
			items.getItems().add("建设单位");
		if (checkBox_2.isSelected())
			items.getItems().add("项目属性");
		if (checkBox_26.isSelected())
			items.getItems().add("企业分类");
		if (checkBox_4.isSelected())
			items.getItems().add("占地");
		if (checkBox_5.isSelected())
			items.getItems().add("注册资本");
		if (checkBox_6.isSelected())
			items.getItems().add("总投资");
		if (checkBox_7.isSelected())
			items.getItems().add("年产值");
		if (checkBox_3.isSelected())
			items.getItems().add("利润");
		if (checkBox_22.isSelected())
			items.getItems().add("税收");
		if (checkBox_8.isSelected())
			items.getItems().add("用工");
		if (checkBox_9.isSelected())
			items.getItems().add("备注");
		if (checkBox_10.isSelected())
			items.getItems().add("简介");
		//		if (checkBox_11.isSelected())
		//			items.getItems().add("手续办理时间");
		if (checkBox_12.isSelected())
			items.getItems().add("注册");
		if (checkBox_13.isSelected())
			items.getItems().add("土地预审");
		if (checkBox_14.isSelected())
			items.getItems().add("备案");
		if (checkBox_15.isSelected())
			items.getItems().add("环评");
		if (checkBox_24.isSelected())
			items.getItems().add("土地出让合同");
		if (checkBox_16.isSelected())
			items.getItems().add("建设用地规划许可");
		if (checkBox_17.isSelected())
			items.getItems().add("建筑工程规划许可");
		if (checkBox_18.isSelected())
			items.getItems().add("施工许可");
		//		if (checkBox_19.isSelected())
		//			items.getItems().add("年度目标");
		if (checkBox_20.isSelected())
			items.getItems().add("完成投资");
		if (checkBox_21.isSelected())
			items.getItems().add("开工面积");
		if (checkBox_23.isSelected())
			items.getItems().add("当前进展");
		Enumeration<AbstractButton> enu1 =  group1.getElements();
		while (enu1.hasMoreElements()) {
			AbstractButton radioButtom = enu1.nextElement(); 
			if (radioButtom.isSelected())
				items.setXiangmushuxing(radioButtom.getText());
		}
		Enumeration<AbstractButton> enu2 =  group2.getElements();
		while (enu2.hasMoreElements()) {
			AbstractButton radioButtom = enu2.nextElement(); 
			if (radioButtom.isSelected())
				items.setQiyefenlei(radioButtom.getText());
		}
		return items;
	}

	private void comboBoxAddRange(JComboBox comboBox, int start, int end) {
		for (int i = start; i <= end; i++)
			comboBox.addItem(i);
	}

	private void exportFile() {
		File file = saveFile();
		if (file == null)
			return;
		int option = 1;
		if (file.exists()) {
			option = JOptionPane.showConfirmDialog(this, "文件已存在，确定覆盖？");
			if (option != 0)
				return;
		}
		String filePath = file.getAbsolutePath();
		if (!filePath.toLowerCase().endsWith(".xls"))
			filePath += ".xls";
		SelectedItems items = buildSelectedItems();
		
		List<Company> companies = null;
		if ("所有".equals(items.getXiangmushuxing()) && "所有".equals(items.getQiyefenlei()))
			companies = dbOperation.findAllCompanies();
		else if (!"所有".equals(items.getXiangmushuxing()) && "所有".equals(items.getQiyefenlei()))
			companies = dbOperation.findCompaniesByXiangmushuxing(items.getXiangmushuxing());
		else if ("所有".equals(items.getXiangmushuxing()) && !"所有".equals(items.getQiyefenlei()))
			companies = dbOperation.findCompaniesByQiyefenlei(items.getQiyefenlei());
		else
			companies = dbOperation.findCompaniesByXiangmushuxingAndQiyefenlei(items.getXiangmushuxing(), items.getQiyefenlei());
		
		if (exp.exportStatistic(filePath, companies, items))
			JOptionPane.showMessageDialog(this, "导出成功！");
		else
			JOptionPane.showMessageDialog(this, "导出失败！请确定该文件没有被其他程序使用。");
		//dispose();
	}

	private File saveFile() {
		File file = null;
		if (fc == null) {
			fc = new JFileChooser();
			//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			//Add a custom file filter and disable the default
			//(Accept All) file filter.
			//fc.addChoosableFileFilter(new ImageFilter());
			//fc.setAcceptAllFileFilterUsed(false);			
			ExcelFileFilter excelFilter = new ExcelFileFilter(); //excel过滤器    
			fc.addChoosableFileFilter(excelFilter);  
			fc.setFileFilter(excelFilter); 
		}

		//Show it.
		int returnVal = fc.showSaveDialog(this);

		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
		}
		//Reset the file chooser for the next time it's shown.
		fc.setSelectedFile(null);
		return file;
	}

	private static void initGlobalFont(){
		FontUIResource fontUIResource = new FontUIResource(new Font("宋体",Font.PLAIN, 12));
		for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value= UIManager.get(key);
			if (value instanceof FontUIResource) {
				UIManager.put(key, fontUIResource);
			}  
		}
	}

}
